<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class User_mdl extends CI_Model{

    const TABLE = 'user';

    /**
     *  表验证规则
     *  1.字段名称
     *  2.正则表达式
     *  3.错误提示信息
     *  4. 1 存在就验证 2.必须验证 3.当值不为空的时候验证
     */
    protected $rules = array(
        //array('username','/^[0-9]{6}&/','提示信息',1,1),
    );

    /**
     * 用户登录
     */
    public function login(){
        //$this->load->library('verify_form');
        $post = $this->input->post(NULL,TRUE);
        //$v = $this->verify_form->verify($post,$this->rules);
        $this->db->select('id,username,mail,nickname,group');
        $this->db->where('username',$post['username']);
        $this->db->where('password',md5($post['password']));
        $query = $this->db->get(self::TABLE);
        $result = $query->row_array();
		
		$this->load->model('logs_mdl','logs');
		if($result['group'] == 0){
			$this->logs->add(2,'用户'.$post['username'].'登陆，因用户被禁用，登陆失败');
			return "该用户已被管理员禁用！";
		}

		//写入日志类
		$this->logs->add(empty($result['id'])?2:1,'用户'.$post['username'].'登陆');

        if(!empty($result['id'])){
            $this->session->set_userdata($result);
            //修改登录时间
            $this->db->update(self::TABLE,array('login_time'=>  time()),array('id'=>$result['id']));
            return true;
        }else{
            return '您的账号或者密码错误！';
        }
    }

    /**
     * 用户修改
     */
    public function edit_pwd($data){
        if(!empty($data['id'])){
            $this->db->where('id',$data['id']);
            $this->db->update(self::TABLE,$data);
            return true;
        }else{
            return false;
        }
    }
    
    /**
     * 创建用户
     */
    public function create($data){
    	return $this->db->insert(self::TABLE,$data);
    }
    
    /**
     * 判断用户是否存在
     */
    public function is_verify($data){
        $this->db->select('id');
        foreach($data as $k => $v){
            $this->db->where($k,$v);
        }
        $query = $this->db->get(self::TABLE);
        $value = $query->row_array();
        if(empty($value)){
        	return true;
        }else{
        	return false;
        }
    }

    /**
     * 获取单个用户资料
     */
    public function get_one($id){
        $this->db->select('id,username,mail,nickname');
        $this->db->where('id',$id);
        $query = $this->db->get(self::TABLE);
        return $query->row_array();
    }
	
	/**
	 * 根据条件查询用户
	 */
	public function get_all($tmp){
		$this->db->select('id,username,mail,nickname,group');
		foreach($tmp as $key => $value){
			$this->db->where($key,$value);
		}
		return $this->db->get(self::TABLE)->result_array();
	}
	
	/**
	  * 用户信息查询
	  * @param $limit	//每页显示多少数据
	  * @param $tmp		//where 条件
	  */
	 public function get_page($limit = 20,$tmp = array()){
	 	$p = $this->input->get('per_page',TRUE);
		foreach ($tmp as $k => $v){
			$this->db->where($k,$v);
		}
		$this->db->select('id,username,mail,nickname,group,login_time,create_time');
	 	$this->db->limit($limit,empty($p)?0:$p);
		$this->db->order_by('Id','DESC');
		$query = $this->db->get(self::TABLE);
		return $query->result_array();
	 }
	 
	 /**
	 * 统计所有用户
	 * @param $tmp	查询条件
	 */
	 public function get_count($tmp = array()){
	 	
	 	foreach($tmp as $k => $v){
	 		$this->db->where($k,$v);
	 	}
		return $this->db->count_all_results(self::TABLE);
	 }
}
